/*   Copyright (C) 2021-2025 sirpdboy herboy2008@gmail.com https://github.com/sirpdboy/luci-app-netspeedtest */
'use strict';
'require view';
'require fs';
'require ui';
'require uci';
'require form';
'require poll';
return view.extend({
    render: function() {
        var state = { 
            running: false,
            port: 3300
        };
        var container = E('div');
        var statusSection = E('div', { 'class': 'cbi-section' });
        var statusIcon = E('span', { 'style': 'margin-right: 5px;' });
        var statusText = E('span');
        var toggleBtn = E('button', { 'class': 'btn cbi-button' });
        
        var statusMessage = E('div', { style: 'text-align: center; padding: 2em;' }, [
            E('img', {
                src: '',
                style: 'width: 100px; height: 100px; margin-bottom: 1em;'
            }),
            E('h2', {}, _('Homebox Service Not Running')),
            E('p', {}, _('Please enable the Homebox service'))
        ]);
        
        var isHttps = window.location.protocol === 'https:';
        var iframe;
        
        if (!isHttps) {
            iframe = E('iframe', {
                src: window.location.origin + ':' + state.port,
                style: 'border:none;width: 100%; min-height: 80vh; border: none; border-radius: 3px;overflow:hidden !important;'
            });
        }

        function createHttpsButton() {
            return E('div', {
                style: 'text-align: center; padding: 2em;'
            }, [
                E('h2', {}, _('Homebox Control panel')),
                E('p', {}, _('Due to browser security policies, the Homebox interface https cannot be embedded directly.')),
                E('a', {
                    href: 'http://' + window.location.hostname + ':' + state.port,
                    target: '_blank',
                    class: 'cbi-button cbi-button-apply',
                    style: 'display: inline-block; margin-top: 1em; padding: 10px 20px; font-size: 16px; text-decoration: none; color: white;'
                }, _('Open Web Interface'))
                
            ]);
        }

        async function checkProcess() {
            try {
                // 尝试使用pgrep 
                const res = await fs.exec('/usr/bin/pgrep', ['homebox']);
                return {
                    running: res.code === 0,
                    pid: res.stdout.trim() || null
                };
            } catch (err) {
                // 回退到ps方法
                try {
                    const psRes = await fs.exec('/bin/ps', ['-w', '-C', 'homebox', '-o', 'pid=']);
                    const pid = psRes.stdout.trim();
                    return {
                        running: pid !== '',
                        pid: pid || null
                    };
                } catch (err) {
                    return { running: false, pid: null };
                }
            }
        }

        function controlService(action) {
            var command = action === 'start' 
                ? 'nohup /usr/bin/homebox >> /tmp/netspeedtest.log 2>&1 &' 
                : '/usr/bin/killall homebox';
            return fs.exec('/bin/sh', ['-c', command]);
        }

        function updateStatus() {
            statusIcon.textContent = state.running ? '✓' : '✗';
            statusIcon.style.color = state.running ? 'green' : 'red';
            statusText.textContent = _('Homebox Server') + (state.running ? _('RUNNING') : _('NOT RUNNING'));
            statusText.style.color = state.running ? 'green' : 'red';
            statusText.style.fontWeight = 'bold'; 
            statusText.style.fontSize = '0.92rem'; 
            
            toggleBtn.textContent = state.running ? _('Stop Server') : _('Start Server');
            toggleBtn.className = `btn cbi-button cbi-button-${state.running ? 'reset' : 'apply'}`;
            
            // Update container content based on state and protocol
            container.textContent = '';
            if (state.running) {
                if (isHttps) {
                    container.appendChild(createHttpsButton());
                } else {
                    container.appendChild(iframe);
                }
            } else {
                container.appendChild(statusMessage);
            }
        }

        toggleBtn.addEventListener('click', ui.createHandlerFn(this, function() {
            var action = state.running ? 'stop' : 'start';
            return controlService(action)
                .then(checkProcess)
                .then(res => {
                    state.running = res.running;
                    updateStatus();
                });
        }));

        statusSection.appendChild(E('div', { 'style': 'margin: 15px' }, [
            E('h3', {}, _('Lan Speedtest Homebox')),
            E('div', { 'class': 'cbi-map-descr' }, [statusIcon, statusText]),
            E('div', {'class': 'cbi-value', 'style': 'margin-top: 20px'}, [
                E('div', {'class': 'cbi-value-title'}, _('Homebox service control')),
                E('div', {'class': 'cbi-value-field'}, toggleBtn),
                E('div', { 'style': 'text-align: right; font-style: italic; margin-top: 20px;' }, [
                    _('© github '),
                    E('a', { 
                        'href': 'https://github.com/sirpdboy', 
                        'target': '_blank',
                        'style': 'text-decoration: none;'
                    }, 'by sirpdboy')
                ])
            ])
        ]));

        // Initial status check
        checkProcess().then(res => {
            state.running = res.running;
            updateStatus();
            toggleBtn.disabled = false;
            // Start polling
            poll.add(() => {
                return checkProcess().then(res => {
                    if (res.running !== state.running) {
                        state.running = res.running;
                        updateStatus();
                        toggleBtn.disabled = false;
                    }
                });
            }, 5);
            
            poll.start();
        });

        return E('div', {}, [
            statusSection,
            container
        ]);
    },

    handleSaveApply: null,
    handleSave: null,
    handleReset: null
});